package ynu.edu.Service.Impl;

import jakarta.annotation.Resource;
import org.hibernate.Hibernate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import ynu.edu.Dao.CameraDao;
import ynu.edu.Dao.DeviceDao;
import ynu.edu.Entity.Camera;
import ynu.edu.Entity.Device;
import ynu.edu.Service.ICameraService;
import ynu.edu.Util.PropertiesUtil;

import java.util.*;

@Service
public class CameraService implements ICameraService {
    @Resource
    DeviceDao deviceDao;
    @Resource
    CameraDao cameraDao;
    @Resource
    PropertiesUtil propertiesUtil;
    @Override
    public void fetchUrl(String url){
        Camera camera = new Camera();
        Device device = deviceDao.getReferenceById(6L);
        camera.setDevice(device);
        camera.setImageUrl(url);

        cameraDao.save(camera);
    }
    @Override
    public String GetToken(){
        Properties properties = propertiesUtil.loadProperties();
        return properties.getProperty("ys_access_token");
    }
    @Override
    @Transactional
    public List<String> getCamerasCreatedInLastMonth() {
        Calendar calendar = Calendar.getInstance();
        calendar.add(Calendar.MONTH, -1);
        Date oneMonthAgo = calendar.getTime();

        List<Camera> cameras = cameraDao.findCamerasCreatedInLastMonth(oneMonthAgo).stream()
                .limit(100)
                .toList();
        List<String> list = new ArrayList<>();
        for (Camera camera : cameras) {
            list.add(camera.getImageUrl());
        }

        return list;
    }
}
